package net.lahuobao.mall.controller.admin;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.JsonObject;
import net.lahuobao.mall.entity.admin.MallDatatypeEntity;
import net.lahuobao.mall.entity.admin.MallProductCategorySpecValEntity;
import net.lahuobao.mall.entity.admin.MallProductSpecChildEntity;
import net.lahuobao.mall.service.admin.MallDatatypeServiceI;
import net.lahuobao.mall.service.admin.MallProductCategorySpecValServiceI;
import net.lahuobao.mall.service.admin.MallProductSpecChildServiceI;
import org.apache.log4j.Logger;
import org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import net.lahuobao.mall.entity.admin.MallProductCategorySpecEntity;
import net.lahuobao.mall.service.admin.MallProductCategorySpecServiceI;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.net.URI;
import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;

/**   
 * @Title: Controller
 * @Description: 商品分类规格
 * @author zhangdaihao
 * @date 2016-10-21 21:29:56
 * @version V1.0   
 *
 */
@Controller
@RequestMapping("/mallProductCategorySpecController")
public class MallProductCategorySpecController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(MallProductCategorySpecController.class);

	@Autowired
	private MallProductCategorySpecServiceI mallProductCategorySpecService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;

	@Autowired
	private MallDatatypeServiceI datatypeService;

	@Autowired
	private MallProductCategorySpecValServiceI valService;

	@Autowired
	private MallProductSpecChildServiceI childService;
	/**
	 * 商品分类规格列表 页面跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "list")
	public ModelAndView list(HttpServletRequest request) {
		return new ModelAndView("net/lahuobao/mall/admin/mallProductCategorySpecList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(MallProductCategorySpecEntity mallProductCategorySpec,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(MallProductCategorySpecEntity.class, dataGrid);
		//查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, mallProductCategorySpec, request.getParameterMap());
		this.mallProductCategorySpecService.getDataGridReturn(cq, false);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除商品分类规格
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(MallProductCategorySpecEntity mallProductCategorySpec, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();

		//判断该规格下是否有规格值
		MallProductCategorySpecValEntity query = new MallProductCategorySpecValEntity();
		CriteriaQuery cq = new CriteriaQuery(MallProductCategorySpecValEntity.class);
		query.setSpecid(mallProductCategorySpec.getId());
		HqlGenerateUtil.installHql(cq, query);
		List<MallProductCategorySpecValEntity> valLists = valService.getListByCriteriaQuery(cq, false);

		if(valLists.size() > 0){
			message = "删除失败！该规格下存在规格值不能删除";
		}else{
			mallProductCategorySpec = systemService.getEntity(MallProductCategorySpecEntity.class, mallProductCategorySpec.getId());
			message = "商品分类规格删除成功";
			mallProductCategorySpecService.delete(mallProductCategorySpec);
			systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加商品分类规格
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(MallProductCategorySpecEntity mallProductCategorySpec, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(mallProductCategorySpec.getId())) {
			message = "商品分类规格更新成功";
			MallProductCategorySpecEntity t = mallProductCategorySpecService.get(MallProductCategorySpecEntity.class, mallProductCategorySpec.getId());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(mallProductCategorySpec, t);
				mallProductCategorySpecService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "商品分类规格更新失败";
			}
		} else {
			message = "商品分类规格添加成功";
			mallProductCategorySpec.setCreatetime(new Date());
			mallProductCategorySpec.setCreateuserid(1);
			mallProductCategorySpecService.save(mallProductCategorySpec);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 商品分类规格列表页面跳转
	 *
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(MallProductCategorySpecEntity mallProductCategorySpec, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(mallProductCategorySpec.getId())) {
			mallProductCategorySpec = mallProductCategorySpecService.getEntity(MallProductCategorySpecEntity.class, mallProductCategorySpec.getId());
		}
		req.setAttribute("mallProductCategorySpecPage", mallProductCategorySpec);
		//查询数据类型
		List<MallDatatypeEntity> lists = datatypeService.getList(MallDatatypeEntity.class);
		req.setAttribute("datatype", lists);
		return new ModelAndView("net/lahuobao/mall/admin/mallProductCategorySpec");
	}

	/**
	 * 根据分类规格名称和规格分类检测唯一行
	 */
	@RequestMapping(params = "chkspecname")
	@ResponseBody
	public void checkSpec(HttpServletRequest request, HttpServletResponse response){
		String cateId = request.getParameter("cateid");
		String specName = request.getParameter("param");
		String info = "该规格名称可用";
		String status = "y";
		//查询
		MallProductCategorySpecEntity query = new MallProductCategorySpecEntity();
		CriteriaQuery cq = new CriteriaQuery(MallProductCategorySpecEntity.class);
		query.setCategoryid(Integer.valueOf(cateId));
		query.setName(specName);
		HqlGenerateUtil.installHql(cq, query);
		List<MallProductCategorySpecEntity> lists = mallProductCategorySpecService.getListByCriteriaQuery(cq, false);
		if(lists.size() > 0){
			info = "该分类已经存在该规格名称";
			status = "n";
		}
		JsonObject json = new JsonObject();
		json.addProperty("info", info);
		json.addProperty("status", status);
		renderData(response, json.toString());
	}


	/**
	 * 判断是否被引用--编辑前
	 *
	 * @return
	 */
	@RequestMapping(params = "isuses")
	@ResponseBody
	public AjaxJson isUses(MallProductCategorySpecEntity mallProductCategorySpec, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();

		//先根据id查询出该规格下的规格值
		MallProductCategorySpecValEntity valEntity = new MallProductCategorySpecValEntity();
		List<MallProductCategorySpecValEntity> valLists = valService.findByProperty(MallProductCategorySpecValEntity.class, "specid", mallProductCategorySpec.getId());

		if(valLists.size() > 0){
			for(int i = 0; i < valLists.size(); i++){
				//判断该规格值是否被引用
				MallProductSpecChildEntity query = new MallProductSpecChildEntity();
				CriteriaQuery cq = new CriteriaQuery(MallProductSpecChildEntity.class);
				query.setCategoryspecvalid(valLists.get(i).getId());
				HqlGenerateUtil.installHql(cq, query);
				List<MallProductSpecChildEntity> childLists = childService.getListByCriteriaQuery(cq, false);
				if(childLists.size() > 0){
					j.setSuccess(false);
					message = "该规格下存在规格值，并且规格值正在被引用不能编辑！";
					break;
				}
			}
		}
		j.setMsg(message);
		return j;
	}

}
